package com.example.hasee.home;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.hasee.FragmentFactory;
import com.example.hasee.addinfo.AddInfoFragment;
import com.example.hasee.base.BaseActivity;
import com.example.hasee.libraryreservation.R;
import com.example.hasee.login.LoginActivity;
import com.example.hasee.showinfo.ShowInfoFragment;
import com.example.hasee.usersetting.UserSettingFragment;
import com.example.hasee.weather.WeatherFragment;

public class HomeActivity extends BaseActivity implements HomeContract.View{
    private DrawerLayout drawerLayout;
    private ListView lvMenu;
    private HomeContract.Presenter mPresenter;
    private FragmentManager mFragmentManager;
    @Override
    protected int setLayout() {
        return R.layout.activity_home;
    }
    @Override
    protected void init() {
        mPresenter=new HomePresenter(this);
        drawerLayout=findViewById(R.id.drawerLayout);
        lvMenu=findViewById(R.id.lvMenu);
        lvMenu.setAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.menu_item, R.id.tvMenu, mPresenter.getFuncMenuList()
        ));
        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.fragmentContainer,new AddInfoFragment()).commit();
    }
    @Override
    protected void setListener() {
        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("MyLog","===="+position);
                switch (position){
                    case 0:
                        changeView(FragmentFactory.creat(AddInfoFragment.class));
                        break;
                    case 1:
                        changeView(FragmentFactory.creat(ShowInfoFragment.class));
                        break;
                    case 2:
                        changeView(FragmentFactory.creat(UserSettingFragment.class));
                        break;
                    case 3:
                        changeView(FragmentFactory.creat(WeatherFragment.class));
                        break;
                    case 4:
                        mPresenter.userLogout();
                        break;
                    default:
                }
                drawerLayout.closeDrawers();
            }
        });
    }
    @Override
    public void changeView(Fragment fragment) {
        mFragmentManager.beginTransaction().replace(R.id.fragmentContainer,fragment).commit();
    }
    @Override
    public void toLoginView() {
        toNewView(LoginActivity.class);
    }
}
